package bb.lanxing.devicelist.widget

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import bb.lanxing.App
import bb.lanxing.R
import bb.lanxing.activity.base.WebActivity
import bb.lanxing.databinding.FragmentBottomDeviceTypeBinding
import bb.lanxing.device.search.SearchDeviceActivity
import bb.lanxing.device.sgdevcie.xpair.XPairLauncher
import bb.lanxing.lib.devices.api.SmartDevice
import bb.lanxing.net.ApiClientConfigs
import bb.lanxing.network.MyHttpClient
import bb.lanxing.util.DensityUtil
import bb.lanxing.util.Log
import bb.lanxing.util.ui.BaseBottomSheetDialogFragment
import com.bes.bessdk.BesSdkConstants
import com.bes.bessdk.service.base.BesServiceConfig
import com.bes.sdk.utils.DeviceProtocol
import com.besall.allbase.common.Constants
import com.besall.allbase.view.activity.chipstoollevel4.ota.OtaUIActivity
import com.besall.allbase.view.activity.chipstoollevel4.protocol.ProtocolActivity
import java.io.Serializable

class DeviceTypeSheetFragment : BaseBottomSheetDialogFragment() {
    private var binding: FragmentBottomDeviceTypeBinding? = null
    private var xPairLauncher: XPairLauncher? = null

    fun sendKey(i: Int) {
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        viewGroup: ViewGroup?,
        bundle: Bundle?
    ): View? {
        val inflate = FragmentBottomDeviceTypeBinding.inflate(inflater, viewGroup, false)
        this.binding = inflate
        binding!!.imgCl600.setOnClickListener { _ ->
/*            val intent =
                Intent(this@DeviceTypeSheetFragment.activity, SearchDeviceActivity::class.java)
            intent.putExtra("EXTRA_DEVICE_TYPE", SmartDevice.TYPE_CHILEAF_600)
            intent.putExtra(SearchDeviceActivity.EXTRA_ADD_NEW, true)*/
            val intent = Intent(requireActivity(), ProtocolActivity::class.java)
            this@DeviceTypeSheetFragment.startActivity(intent)
            this@DeviceTypeSheetFragment.dismiss()
            this@DeviceTypeSheetFragment.sendKey(SmartDevice.TYPE_CHILEAF_600)
        }

        binding!!.imgXossNav.setOnClickListener { v: View? ->
            if (xPairLauncher != null) {
                xPairLauncher!!.launchForNav()
            }
            this@DeviceTypeSheetFragment.dismiss()
            this@DeviceTypeSheetFragment.sendKey(SmartDevice.TYPE_XOSS_NAV)
        }

        binding!!.imgXossSprint.setOnClickListener { v: View? ->
            val intent =
                Intent(this@DeviceTypeSheetFragment.activity, SearchDeviceActivity::class.java)
            intent.putExtra("EXTRA_DEVICE_TYPE", SmartDevice.TYPE_WINGS)
            intent.putExtra(SearchDeviceActivity.EXTRA_ADD_NEW, true)
            this@DeviceTypeSheetFragment.startActivity(intent)
            this@DeviceTypeSheetFragment.dismiss()
            this@DeviceTypeSheetFragment.sendKey(SmartDevice.TYPE_WINGS)
        }

        binding!!.imgXossG.setOnClickListener { v: View? ->
            val intent =
                Intent(this@DeviceTypeSheetFragment.activity, SearchDeviceActivity::class.java)
            intent.putExtra("EXTRA_DEVICE_TYPE", SmartDevice.TYPE_XOSS_G)
            intent.putExtra(SearchDeviceActivity.EXTRA_ADD_NEW, true)
            this@DeviceTypeSheetFragment.startActivity(intent)
            this@DeviceTypeSheetFragment.dismiss()
            this@DeviceTypeSheetFragment.sendKey(SmartDevice.TYPE_XOSS_G)
        }

        binding!!.imgXplova.setOnClickListener { v: View? ->
            val signinUser = App.getContext().signinUser
            if (signinUser == null) {
                App.getContext().userSignin()
                return@setOnClickListener
            }
            val str = (ApiClientConfigs.PROTOCOL_HTTP + MyHttpClient.DOMAIN_BASE
                    + "/partner/xplova/#/index?no=" + signinUser.uid)
            Log.e("xplova", str)
            val putExtra = Intent(this@DeviceTypeSheetFragment.activity, WebActivity::class.java)
                .putExtra("web_url", str)
                .putExtra(
                    "title",
                    this@DeviceTypeSheetFragment.getString(R.string.device_xplova_xz_title)
                )
                .putExtra("homeBackable", true)
                .putExtra("can_share", false)
            this@DeviceTypeSheetFragment.startActivity(putExtra)
        }

        binding!!.imgCycplusM1.setOnClickListener { v: View? ->
            val intent =
                Intent(this@DeviceTypeSheetFragment.activity, SearchDeviceActivity::class.java)
            intent.putExtra("EXTRA_DEVICE_TYPE", SmartDevice.TYPE_CYCLE_PLUS_M1)
            intent.putExtra(SearchDeviceActivity.EXTRA_ADD_NEW, true)
            startActivity(intent)
            dismiss()
            sendKey(SmartDevice.TYPE_CYCLE_PLUS_M1)
        }
        binding!!.imgCycplusM2.setOnClickListener { v: View? ->
            val intent =
                Intent(this@DeviceTypeSheetFragment.activity, SearchDeviceActivity::class.java)
            intent.putExtra("EXTRA_DEVICE_TYPE", SmartDevice.TYPE_CYCLE_PLUS_M2)
            intent.putExtra(SearchDeviceActivity.EXTRA_ADD_NEW, true)

            this@DeviceTypeSheetFragment.startActivity(intent)
            this@DeviceTypeSheetFragment.dismiss()
            this@DeviceTypeSheetFragment.sendKey(SmartDevice.TYPE_CYCLE_PLUS_M2)
        }
        binding!!.imgWatchOta.setOnClickListener { v: View? ->
            val intent =
                Intent(
                    this@DeviceTypeSheetFragment.activity,
                    OtaUIActivity::class.java
                ) //com.besall.allbase.app.LaunchActivity //DemoActivity
            //  val intent = Intent(requireActivity(), OtaUIActivity::class.java)
            val serviceConfig4 = BesServiceConfig()
            serviceConfig4.deviceProtocol = DeviceProtocol.PROTOCOL_BLE
            serviceConfig4.serviceUUID = BesSdkConstants.BES_OTA_SERVICE_OTA_UUID
            serviceConfig4.characteristicsUUID = BesSdkConstants.BES_OTA_CHARACTERISTIC_OTA_UUID
            serviceConfig4.descriptorUUID = BesSdkConstants.BES_OTA_DESCRIPTOR_OTA_UUID
            serviceConfig4.useR_FLAG = 1
            intent.putExtra(Constants.OTA_SERVICE_CONFIG, serviceConfig4 as Serializable)
            //intent.putExtra("EXTRA_DEVICE_TYPE", SmartDevice.TYPE_CYCLE_PLUS_M2)
            //intent.putExtra(SearchDeviceActivity.EXTRA_ADD_NEW, true)
            this@DeviceTypeSheetFragment.startActivity(intent)
            this@DeviceTypeSheetFragment.dismiss()
            this@DeviceTypeSheetFragment.sendKey(SmartDevice.TYPE_WATCH_OTA)
        }
        return inflate.root
    }

    override fun getTopOffset(): Int {
        return DensityUtil.dp2px(50.0f)
    }

    companion object {
        const val ARG_PARAM_DEVICE_TYPE: String = "device_type"
        fun newInstance(xPairLauncher: XPairLauncher): DeviceTypeSheetFragment {
            val deviceTypeSheetFragment = DeviceTypeSheetFragment()
            deviceTypeSheetFragment.xPairLauncher = xPairLauncher
            return deviceTypeSheetFragment
        }
    }
}
